package io.renren.modules.canteens.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.canteens.dao.CollarmxDao;
import io.renren.modules.canteens.dto.*;
import io.renren.modules.canteens.dto.CollarmxDTO;
import io.renren.modules.canteens.entity.CollarmxEntity;
import io.renren.modules.canteens.service.CollarService;
import io.renren.modules.canteens.service.CollarmxService;
import io.renren.modules.canteens.service.FoodService;
import io.renren.modules.sys.service.SysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 辅材领用明细表
 *
 * @author 罗胜平 781757616@qq.com
 * @since 1.0.0 2019-11-22
 */
@Service
public class CollarmxServiceImpl extends CrudServiceImpl<CollarmxDao, CollarmxEntity, CollarmxDTO> implements CollarmxService {
    @Autowired
    private FoodService foodService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private CollarService collarService;
    @Autowired
    private CollarmxDao collarmxDao;
    @Override
    public QueryWrapper<CollarmxEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<CollarmxEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public PageData<CollarmxDTO> getOrderMx(Map<String, Object> params) {
        QueryWrapper<CollarmxEntity> wrapper = new QueryWrapper<>();
        String collarId =  (String)params.get("collarId");

        wrapper.eq(StringUtils.isNotBlank(collarId), "collar_id", collarId);
        IPage<CollarmxEntity> page = baseDao.selectPage(
                getPage(params, null, false),
                wrapper);
        PageData<CollarmxDTO> pageData = getPageData(page, CollarmxDTO.class);
        for (CollarmxDTO ordermxDTO:pageData.getList()) {
            FoodDTO foodDTO = foodService.get(ordermxDTO.getFoodId());
            if(null != foodDTO){
                ordermxDTO.setFoodName(foodDTO.getFoodName());
                ordermxDTO.setUnit(foodDTO.getUnit());
            }
        }
        return pageData;
    }

    @Override
    public List<CollarmxDTO> getOrderMx1(Map<String, Object> params) {
        String stockOrderId1 =  (String)params.get("stockOrderId");
        List<CollarmxDTO> list = new ArrayList<>();
        if(StringUtils.isNotBlank(stockOrderId1)) {
            long stockOrderId = Long.parseLong(stockOrderId1);
            CollarDTO collarDTO = collarService.get(stockOrderId);
            //collarmxDao.getOrderMx()


        }








   /*         List<CollarmxDTO> stockOrdermxDTOList = collarmxDao.getsupplierOrder(stockOrderId);
            for (CollarmxDTO ordermxDTO:stockOrdermxDTOList) {
                List<CollarmxDTO> orderMx = collarmxDao.getOrderMx(stockOrderId, ordermxDTO.getSupplierId());
                for (CollarmxDTO ordermxDTO1:orderMx) {
                    ordermxDTO1.setTotalPrice1(String.valueOf(ordermxDTO1.getTotalPrice()));
                    list.add(ordermxDTO1);
                }
                CollarmxDTO totalPrice = collarmxDao.getTotalPrice(stockOrderId, ordermxDTO.getSupplierId());
                CollarmxDTO ordermxDTO1 = new CollarmxDTO();
                ordermxDTO1.setMealTime(collarDTO.getCreateDate());
                ordermxDTO1.setTotalPrice1("合计 ： "+ totalPrice.getTotalPrice());
                list.add(ordermxDTO1);
            }
            CollarmxDTO totalPrice = collarmxDao.getTotalMony(collarDTO.getCollarId());
            CollarmxDTO ordermxDTO = new CollarmxDTO();
            ordermxDTO.setMealTime(collarDTO.getCreateDate());
            ordermxDTO.setTotalPrice1("共计 ： "+totalPrice.getTotalPrice());
            list.add(ordermxDTO);

            String name = sysDeptService.get(collarDTO.getCanteenId()).getName();
            for (CollarmxDTO mxDTO:list) {
                if(StringUtils.isNotBlank(mxDTO.getSupplierName())){
                    BigDecimal unitPrice = new BigDecimal(mxDTO.getUnitPrice());
                    BigDecimal rrivalNum = new BigDecimal(mxDTO.getArrivalNum());
                    DecimalFormat df2 =new DecimalFormat("#.00");
                    mxDTO.setTotalPrice1(df2.format(unitPrice.multiply(rrivalNum)));
                    mxDTO.setCanteenName(name+"每日进货清单");
                }
            }
        }*/
        return list;

    }
}